import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

const roleList = [
    { name: '管理员' },
    { name: '普通用户' },
    { name: 'vip用户' }
]

async function init() {
    // 创建角色
    for (const role of roleList) {
        await prisma.role.create({
            data: {
                name: role.name
            }
        })
    }
    // 创建用户
    await prisma.user.create({
        data: {
            username: 'admin',
            password: 'admin',
            email: '',
            nickName: "逍遥子",
            avatar: "https://img1.baidu.com/it/u=1333417867,4012964063&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1714496400&t=acbd289a0ec893e2560c39246a0d35b2"
        }
    })
    // 给初始化用户分配管理员
    const user = await prisma.user.findFirst({
        where: {
            username: 'admin'
        }
    })
    const role = await prisma.role.findFirst({
        where: {
            name: '管理员'
        }
    })
    await prisma.userRole.create({
        data: {
            userId: user.id,
            roleId: role.id
        }
    })
}

init()
    .catch(e => {
        throw e
    })
    .finally(async () => {
        await prisma.$disconnect()
    })